﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleConsumeAstoria {
    /// <summary>
    /// Sử dụng WCF Service, gọi method cung cấp bởi WCF Service. Method này sẽ tương tác với EF4.
    /// Lập trình khá thuận tiện
    /// </summary>
    class Demo {
        public void getAnArtist(int id) {
            using (var client = new ServiceClient.MusicServiceClient()) {
                ServiceClient.Artist artist = client.getAnArtist(id);
                Console.WriteLine("{0}", artist.Name);
                foreach (var item in artist.Albums) {
                    Console.WriteLine("{0}", item.Title);
                }
            }
        }

        public void ChangeNameOfAnArtist(int id, string newName) {
            using (var client = new ServiceClient.MusicServiceClient()) {
                client.EditArtistName(id, newName);
                ServiceClient.Artist artist = client.getAnArtist(id);
                Console.WriteLine("{0}", artist.Name);
            }
        }

        public void UpdateArtist(int id, string newName) {
            using (var client = new ServiceClient.MusicServiceClient()) {
                ServiceClient.Artist artist = client.getAnArtist(id);
                artist.Name = newName;
                client.UpdateArtist(artist);
                var test = client.getAnArtist(id);
                if (test.Name == newName) {
                    Console.WriteLine("Update Successfully");
                }
            }
        }

        public void RunSQL() {
            using (var client = new ServiceClient.MusicServiceClient()) {
                ServiceClient.GenreGroup[] result = client.runSQLQuery(@"SELECT Genre.Name, GenreGroup.count_num FROM 
                (SELECT GenreID, COUNT(*) AS count_num FROM Track GROUP BY GenreID) AS GenreGroup
                JOIN Genre ON Genre.GenreId = GenreGroup.GenreId");
                foreach (var item in result) {
                    Console.WriteLine("{0} - {1}", item.Name, item.count_num);
                }

            }
        }
        /// <summary>
        /// Thử chạy một câu lệnh Dynamic SQL gửi từ client. Method này sẽ raise exception        
        /// </summary>
        public void RunDynamicSQL() {
            using (var client = new ServiceClient.MusicServiceClient()) {
                dynamic result = client.runDynamicSQLQuery(@"SELECT Genre.Name, GenreGroup.count_num FROM 
                (SELECT GenreID, COUNT(*) AS count_num FROM Track GROUP BY GenreID) AS GenreGroup
                JOIN Genre ON Genre.GenreId = GenreGroup.GenreId");
                foreach (var item in result) {
                    Console.WriteLine("{0} - {1}", item.Name, item.count_num);
                }

            }
        }


    }
}
